/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sale.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sale.entity.SaleTask;
import com.jeeplus.modules.sale.mapper.SaleTaskMapper;
import com.jeeplus.modules.sale.entity.SaleContacts;
import com.jeeplus.modules.sale.mapper.SaleContactsMapper;
import com.jeeplus.modules.sale.entity.SalePartner;
import com.jeeplus.modules.sale.mapper.SalePartnerMapper;
import com.jeeplus.modules.sale.entity.SaleRecord;
import com.jeeplus.modules.sale.mapper.SaleRecordMapper;

/**
 * 销售任务Service
 * @author 卢经纬
 * @version 2018-06-30
 */
@Service
@Transactional(readOnly = true)
public class SaleTaskService extends CrudService<SaleTaskMapper, SaleTask> {

	@Autowired
	private SaleContactsMapper saleContactsMapper;
	@Autowired
	private SalePartnerMapper salePartnerMapper;
	@Autowired
	private SaleRecordMapper saleRecordMapper;
	
	public SaleTask get(String id) {
		SaleTask saleTask = super.get(id);
		SaleContacts saleContacts = new SaleContacts(saleTask);
		saleTask.setSaleContactsList(saleContactsMapper.findList(saleContacts));
		saleTask.setSalePartnerList(salePartnerMapper.findList(new SalePartner(saleTask)));
		saleTask.setSaleRecordList(saleRecordMapper.findList(new SaleRecord(saleTask)));
		return saleTask;
	}
	
	public List<SaleTask> findList(SaleTask saleTask) {
		return super.findList(saleTask);
	}
	
	public Page<SaleTask> findPage(Page<SaleTask> page, SaleTask saleTask) {
		return super.findPage(page, saleTask);
	}
	
	@Transactional(readOnly = false)
	public void save(SaleTask saleTask) {
		super.save(saleTask);
		for (SaleContacts saleContacts : saleTask.getSaleContactsList()){
			if (SaleContacts.DEL_FLAG_NORMAL.equals(saleContacts.getDelFlag())){
				if (StringUtils.isBlank(saleContacts.getId())){
					saleContacts.setSaleTask(saleTask);
					saleContacts.preInsert();
					saleContactsMapper.insert(saleContacts);
				}else{
					saleContacts.preUpdate();
					saleContactsMapper.update(saleContacts);
				}
			}else{
				saleContactsMapper.delete(saleContacts);
			}
		}
		for (SalePartner salePartner : saleTask.getSalePartnerList()){
			if (SalePartner.DEL_FLAG_NORMAL.equals(salePartner.getDelFlag())){
				if (StringUtils.isBlank(salePartner.getId())){
					salePartner.setSaleTask(saleTask);
					salePartner.preInsert();
					salePartnerMapper.insert(salePartner);
				}else{
					salePartner.preUpdate();
					salePartnerMapper.update(salePartner);
				}
			}else{
				salePartnerMapper.delete(salePartner);
			}
		}
		for (SaleRecord saleRecord : saleTask.getSaleRecordList()){
			if (SaleRecord.DEL_FLAG_NORMAL.equals(saleRecord.getDelFlag())){
				if (StringUtils.isBlank(saleRecord.getId())){
					saleRecord.setSaleTask(saleTask);
					saleRecord.preInsert();
					saleRecordMapper.insert(saleRecord);
				}else{
					saleRecord.preUpdate();
					saleRecordMapper.update(saleRecord);
				}
			}else{
				saleRecordMapper.delete(saleRecord);
			}
		}
	}

	@Transactional(readOnly = false)
	public void saveRecord(SaleTask saleTask) {
		for (SaleRecord saleRecord : saleTask.getSaleRecordList()){
			if (SaleRecord.DEL_FLAG_NORMAL.equals(saleRecord.getDelFlag())){
				if (StringUtils.isBlank(saleRecord.getId())){
					saleRecord.setSaleTask(saleTask);
					saleRecord.preInsert();
					saleRecordMapper.insert(saleRecord);
				}else{
					saleRecord.preUpdate();
					saleRecordMapper.update(saleRecord);
				}
			}else{
				saleRecordMapper.delete(saleRecord);
			}
		}
	}


	@Transactional(readOnly = false)
	public void delete(SaleTask saleTask) {
		super.delete(saleTask);
		saleContactsMapper.delete(new SaleContacts(saleTask));
		salePartnerMapper.delete(new SalePartner(saleTask));
		saleRecordMapper.delete(new SaleRecord(saleTask));
	}
	
}